package org.oexam.ostudy.sl;

import org.oexam.ostudy.db.UsersAccess;
import org.oexam.ostudy.struct.Users;
import org.oexam.ostudy.facade.struct.LoggedUserDetails;

public class Authentication {

	public LoggedUserDetails authenticate(String userName, String password) {

		UsersAccess userDetails = new UsersAccess();
		Users user = userDetails.readUser(userName);

		LoggedUserDetails loggedUserDetails = new LoggedUserDetails();

		if (null==user) {
			loggedUserDetails.setAuthenticated(false);
			loggedUserDetails.setUserName(userName);

			return loggedUserDetails;
		}

		if (!(password.equals(user.getPassword()))) {

			loggedUserDetails.setAuthenticated(false);
			loggedUserDetails.setUserName(userName);

			return loggedUserDetails;
		}

//      Code block to read all details of users based on the user type. 
		
//		if (user.getUser_type() == 0) {
//
//		} else if (user.getUser_type() == 1) {
//
//		} else if (user.getUser_type() == 2) {
//
//		}
		

		
		loggedUserDetails.setAuthenticated(user.isActive());
		loggedUserDetails.setName(user.getUser_name());
		loggedUserDetails.setUserName(user.getUser_name());
		loggedUserDetails.setUserType(user.getUser_type());
		loggedUserDetails.setUserID(user.getUser_id());

		return loggedUserDetails;
	}

}
